<?php

use App\Common\Constants\CacheConst;
use App\Models\System\SysDictDataModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;

/**
 * 字典工具类
 *
 * @date 2023-09-26
 */
class DictUtils {

    /**
     * 分隔符
     */
    const SEPARATOR = ",";


    /**
     * 设置cache key
     * 
     * @param string $configKey 参数键
     * @return string 缓存键key
     */
    public static function getCacheKey(string $configKey): string { return CacheConst::SYS_DICT_KEY() . $configKey; }
    private static function getCacheEx() { return now()->addDay(); }

    /**
     * 设置字典缓存
     * 
     * @param string $key 参数键
     * @param Collection|SysDictDataModel[] 字典数据列表
     */
    public static function setDictCache(string $key, Collection $dictDatas) {
        redis([static::getCacheKey($key) => $dictDatas->toJson()], static::getCacheEx());
    }

    /**
     * 获取字典缓存
     * 
     * @param string $key 参数键
     * @return array|SysDictDataModel[] 字典数据列表
     */
    public static function getDictCache(string $key): array {
        $arrayCache = redis(static::getCacheKey($key));
        return json_decode($arrayCache, true);
    }

    /**
     * 根据字典类型和字典值获取字典标签
     * 
     * @param $dictType 字典类型
     * @param $dictValue 字典值
     * @param $separator 分隔符
     * @return string 字典标签
     */
    public static function getDictLabel(string $dictType, string $dictValue, string $separator=static::SEPARATOR): string {
        $propertyString = collect();
        $datas = static::getDictCache($dictType);
        if (strpos($dictValue, $separator) !== false) {
            foreach($datas as $dict) {
                foreach(explode($separator, $dictValue) as $value) {
                    if ($value == $dict['dict_value']) {
                        $propertyString->push($dict['dict_label']);
                        break;
                    }
                }
            }
        } else {
            foreach($datas as $dict) {
                if ($dictValue == $dict['dict_value']) {
                    $propertyString->push($dict['dict_label']);
                }
            }
        }

        return $propertyString->join($separator);
    }

    /**
     * 根据字典类型和字典标签获取字典值
     * 
     * @param string $dictType 字典类型
     * @param string $dictLabel 字典标签
     * @param string $separator 分隔符
     * @return string 字典值
     */
    public static function getDictValue(string $dictType, string $dictLabel, string $separator=static::SEPARATOR): string {
        $propertyString = collect();
        $datas = static::getDictCache($dictType);
        if (strpos($dictLabel, $separator) !== false) {
            foreach($datas as $dict) {
                foreach(explode($separator, $dictLabel) as $value) {
                    if ($value == $dict['dict_label']) {
                        $propertyString->push($dict['dict_value']);
                        break;
                    }
                }
            }
        } else {
            foreach($datas as $dict) {
                if ($dictLabel == $dict['dict_label']) {
                    $propertyString->push($dict['dict_value']);
                }
            }
        }

        return $propertyString->join($separator);
    }

    /**
     * 删除指定字典缓存
     * 
     * @param string $key 字典键
     */
    public static function removeDictCache(string $key): int {
        return redis(static::getCacheKey($key), 'del');
    }

    /**
     * 清空字典缓存
     */
    public static function clearDictCache(): void {
        foreach(redis(static::getCacheKey('*'), 'keys') as $key) {
            redis($key, 'del');
        }
    }



} 